<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.List" %>
<%@ page import="com.cby.ssm.entity.UserEntity" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="com.cby.ssm.entity.RoleEntity" %>
<%@ page import="com.cby.ssm.entity.User_RoleEntity" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>delectUser</title>
    <style>
        .one{
            line-height:40px;
            text-align: center;
            background-color: aliceblue;
        }
        .two{
            height: 500px;
        }
    </style>
    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
    <script src="assets/jquery/jquery-3.1.1.min.js"></script>
    <script src="assets/bootstrap/js/bootstrap.min.js"></script>
    <script>
        function delete_confirm() {
            event.returnValue = confirm("删除是不可恢复的，你确认要删除吗？");
        }
    </script>
</head>
<body>
<div align="center">
    <header class="one">
        <form action="searchUser.do" method="post">
                <span>
                    <a href="addUser.do"><button class="btn btn-danger" type="button" ><span class="glyphicon glyphicon-plus"></span>添加</button></a>
                    <select class="btn btn-primary">
                        <option value ="5" name="searchMethod">模糊搜索</option>
                        <option value ="1" name="searchMethod">id</option>
                        <option value ="2" name="searchMethod">账户</option>
                        <option value ="3" name="searchMethod">名字</option>
                        <option value ="4" name="searchMethod">电话</option>
                    </select>
                    <input name="pn" value="1" type="hidden">
                    <input name="searchKey" type="text" class="btn btn-default" value="${searchKey}" placeholder="Search">
                    <button type="submit" class="btn btn-info"><span class="glyphicon glyphicon-search"></span>查询</button>
                    <a href="deleteUser.do?pn=1"><button type="button" class="btn btn-warning"><span class="glyphicon glyphicon-refresh"></span>重置条件</button></a>
                </span>
        </form>
    </header>
    <section >
        <c:set var="mes" value="${message}"></c:set>
        <c:if test="${mes.equals('notDelete')}">
            <p>该用户不能删除！</p>
        </c:if>
        <!form method="get" action="deleteUser.do">
            <table class="table table-hover two" >
                <thead>
                <tr class="info">
                    <th>#</th>
                    <th>注册时间</th>
                    <th>账户</th>
                    <th>名字</th>
                    <th>性别</th>
                    <th>电话</th>
                    <th>邮箱</th>
                    <th>地址</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <%
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                    int pageCount = (int)request.getAttribute("pageCount");
                    int pn =(int)request.getAttribute("pn");
                    int pnT =pn;
                    if(pn%3==0){
                        pn=1+(pn/3-1)*3;
                    }else{
                        pn=1+pn/3*3;
                    }
                    int count = 0;
                    List<UserEntity> list = new ArrayList<>();
                    list.addAll((List<UserEntity>)request.getAttribute("pageList"));
                    for(UserEntity u:list){
                        count++;
                %>
                <tr>
                    <td class="success">#<%=(pnT-1)*10+count%></td>
                    <td><%=sdf.format(u.getEnrollDate())%></td>
                    <td><%=u.getAccount()%></td>
                    <td><%=u.getName()%></td>
                    <td><%=u.getSex()%></td>
                    <td><%=u.getTelephone()%></td>
                    <td><%=u.getEmail()%></td>
                    <td><%=u.getAddress()%></td>
                    <td>
                        <!-- 分配角色模态框（Modal）start -->
                        <div class="modal fade" id="myModal<%=u.getId()%>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel<%=u.getId()%>" aria-hidden="true">
                            <div class="modal-dialog" style="width: 30%">
                                <div class="modal-content" style="background-color: #afd9ee">
                                    <div class="modal-header" >
                                        <h4 class="modal-title" id="myModalLabel<%=u.getId()%>">
                                            请选择<strong><%=u.getName()%></strong>需要分配的角色
                                        </h4>
                                    </div>
                                    <form class="form-group" method="post" action="allocateRoles.do">
                                        <div class="modal-body">
                                            <table class="table">
                                                <thead>
                                                <tr>
                                                    <th>选择</th>
                                                    <th>角色名称</th>
                                                </tr>
                                                </thead>
                                                <tbody>
                                                <%
                                                    List<RoleEntity> rlist = new ArrayList<>();
                                                    rlist.addAll((List<RoleEntity>)request.getAttribute("allRoles"));
                                                    for(RoleEntity r:rlist){
                                                %>
                                                <tr>
                                                    <%
                                                        String check = null;
                                                        List<User_RoleEntity> urList  = new ArrayList<>();
                                                        urList.addAll((List<User_RoleEntity>)request.getAttribute("roleListOfUserId"+u.getId()));
                                                        for (User_RoleEntity ur:urList){
                                                            if(r.getRoleId().equals(ur.getRoleId())){
                                                                check = "checked";
                                                                break;
                                                            }
                                                        }
                                                    %>
                                                    <td><input type="checkbox"  value=<%=r.getRoleId()%> <%=check%> name="choose" ></td>
                                                    <td><%=r.getRoleName()%></td>
                                                </tr>
                                                <%
                                                    }
                                                %>
                                                </tbody>
                                            </table>
                                        </div>
                                        <div class="modal-footer">
                                            <input type="hidden" name="action" value="searchUser">
                                            <input type="hidden" name="searchKey" value="${searchKey}">
                                            <input type="hidden" name="pn" value="<%=pnT%>">
                                            <input type="hidden" name="userId" value="<%=u.getId()%>">
                                            <button type="submit" class="btn btn-primary">提交</button>
                                            <button type="button" class="btn btn-danger" data-dismiss="modal">关闭</button>
                                        </div>
                                    </form>
                                </div><!-- /.modal-content -->
                            </div><!-- /.modal -->
                        </div>
                        <!-- 分配角色模态框（Modal）end -->
                        <a>
                            <button class="btn btn-primary" type="button" data-toggle="modal" data-target="#myModal<%=u.getId()%>">
                                <span class="glyphicon glyphicon-user">分配角色</span>
                            </button>
                        </a>
                        <a href="deleteUserOfSearchView.do?id=<%=u.getId()%>&pn=<%=pnT%>&searchKey=${searchKey}" target="mainFrame" onclick="delete_confirm()">
                            <button class="btn btn-danger" type="button"><span class="glyphicon glyphicon-remove-circle">删除</span></button>
                        </a>
                        <!--action==1位查询-->
                        <a href="editUser.do?id=<%=u.getId()%>&pn=<%=pnT%>&action=searchUser&searchKey=${searchKey}" target="mainFrame">
                            <button class="btn btn-success" type="button"><span class="glyphicon glyphicon-pencil">修改</span></button>
                        </a>
                        <a href="welcome.do" target="mainFrame">
                            <button class="btn btn-warning" type="button"><span class="glyphicon glyphicon-zoom-in">查看</span></button>
                        </a>
                    </td>
                </tr>
                <%
                    }
                %>
                </tbody>
            </table>
        <!/form>
    </section>
    <footer>
        <ul class="pagination">
            <li><a href="searchUser.do?pn=1&searchKey=${searchKey}">首页</a></li>
            <li><a href="searchUser.do?pn=<%=pn-1==0?1:pn-1%>&searchKey=${searchKey}" target="mainFrame"><<</a></li>
            <li><a href="searchUser.do?pn=<%=pn%>&searchKey=${searchKey}" target="mainFrame"><%=pn%></a></li>
            <li><a href="searchUser.do?pn=<%=pn+1>pageCount?pn:pn+1%>&searchKey=${searchKey}" target="mainFrame"><%=pn+1%></a></li>
            <li><a href="searchUser.do?pn=<%=pn+2>pageCount?(pn+1>pageCount?pn:pn+1):pn+2%>&searchKey=${searchKey}" target="mainFrame"><%=pn+2%></a></li>
            <li><a href="searchUser.do?pn=<%=pn+3>pageCount?pn:pn+3%>&searchKey=${searchKey}" target="mainFrame">>></a></li>
            <li><a href="searchUser.do?pn=<%=pageCount%>&searchKey=${searchKey}">末页</a></li>
            <li><a>总页数：<%=pageCount%></a></li>
        </ul>
    </footer>
</div>
<%
    session.removeAttribute("message");
%>
</body>
</html>
